<template>
  <!-- 增加修改 -->
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <!-- Name -->
      <a-form-model-item label="名字" prop="name">
        <a-input v-model="form.name" placeholder="请输入名字" :maxLength="30" />
      </a-form-model-item>

      <!-- Department -->
<!--      <a-form-model-item label="部门" prop="deptId">-->
<!--        <a-tree-select-->
<!--          v-model="form.deptId"-->
<!--          style="width: 100%"-->
<!--          :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"-->
<!--          :tree-data="deptOptions"-->
<!--          placeholder="请选择"-->
<!--          :replaceFields="replaceFields"-->
<!--          tree-default-expand-all-->
<!--        >-->
<!--        </a-tree-select>-->
<!--      </a-form-model-item>-->

      <!-- Gender -->
      <a-form-model-item label="性别" prop="sex">
        <a-radio-group v-model="form.sex" button-style="solid">
          <a-radio-button v-for="(d, index) in sexOptions" :key="index" :value="d.value">{{ d.label }}</a-radio-button>
        </a-radio-group>
      </a-form-model-item>

      <!-- Birthday -->
      <a-form-model-item label="出生日期" prop="birthday">
        <a-date-picker v-model="form.birthday" style="width: 100%" placeholder="请选择出生日期" />
      </a-form-model-item>

      <!-- Phone -->
      <a-form-model-item label="手机号码" prop="phone">
        <a-input v-model="form.phone" placeholder="请输入联系方式手机号码" />
      </a-form-model-item>

      <!-- Email -->
      <a-form-model-item label="邮箱" prop="email">
        <a-input v-model="form.email" placeholder="请输入联系方式邮箱" />
      </a-form-model-item>

      <!-- Hire Date -->
      <a-form-model-item label="入职时间" prop="hireDate">
        <a-date-picker v-model="form.hireDate" style="width: 100%" placeholder="请选择入职时间" />
      </a-form-model-item>

      <!-- Remarks -->
      <a-form-model-item label="备注" prop="remark">
        <a-input v-model="form.remark" placeholder="请输入备注" type="textarea" allow-clear />
      </a-form-model-item>

      <!-- Control Buttons -->
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { getStaff, addStaff, updateStaff } from '@/api/system/staff'

export default {
  name: 'CreateForm',
  props: {
    deptOptions: {
      type: Array,
      required: true
    },
    sexOptions: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      submitLoading: false,
      replaceFields: { children: 'children', title: 'label', key: 'id', value: 'id' },
      formTitle: '',
      open: false,
      // Form parameters for staff
      form: {
        staffId: undefined,
        name: undefined,
        deptId: undefined,
        sex: undefined,
        birthday: null,
        phone: undefined,
        email: undefined,
        hireDate: null,
        remark: undefined
      },
      rules: {
        name: [{ required: true, message: '名字不能为空', trigger: 'blur' }],
        // deptId: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        hireDate: [{ required: true, message: '请选择入职时间', trigger: 'change' }]
      }
    }
  },
  created () {
    // Fetch initial data or configuration if needed
  },
  methods: {
    onClose () {
      this.open = false
    },
    cancel () {
      this.open = false
      this.reset()
    },
    reset () {
      this.form = {
        staffId: undefined,
        name: undefined,
        deptId: undefined,
        sex: undefined,
        birthday: null,
        phone: undefined,
        email: undefined,
        hireDate: null,
        remark: undefined
      }
    },
    handleAdd () {
      this.reset()
      this.$emit('select-tree')
      this.open = true
    },
    handleUpdate (staffId) {
      this.reset()
      this.$emit('select-tree')
      getStaff(staffId).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改员工'
      })
    },
    submitForm () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.staffId !== undefined) {
            updateStaff(this.form).then(response => {
              this.$message.success('修改成功')
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addStaff(this.form).then(response => {
              this.$message.success('新增成功')
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.bottom-control {
  margin-top: 20px;
}
</style>
